package com.fas.system.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.fas.common.core.redis.RedisCache;
import com.fas.common.utils.Arith;
import com.fas.common.utils.DateUtils;
import com.fas.system.domain.FasInspectionPlan;
import com.fas.system.domain.FasInspectionTask;
import com.fas.system.mapper.FasInspectionTaskMapper;
import com.fas.system.service.IFasInspectionTaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class FasInspectionTaskServiceImpl implements IFasInspectionTaskService {

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    @Resource
    private FasInspectionTaskMapper fasInspectionTaskMapper;
    @Resource
    private RedisCache redisCache;

    @Override
    public int insertFasInspectionTask(FasInspectionPlan fasInspectionPlan) {

        Date createTime = new Date();
        Snowflake snowflake = IdUtil.getSnowflake();
        Date startDate = fasInspectionPlan.getStartDate();
        Date endDate = fasInspectionPlan.getEndDate();
        List<String> intervalList = fasInspectionPlan.getIntervalList();
        Long lineId = fasInspectionPlan.getLineId();
        Integer deptId = fasInspectionPlan.getDeptId();

        List<FasInspectionTask> list = new ArrayList<>();
        List<String> betweenDate = getBetweenDate(startDate, endDate);
        for (String s : betweenDate) {
            for (String interval : intervalList) {//08:00-12:00
//                long id = snowflake.nextId();  雪花算法
                long id = System.currentTimeMillis();// 时间戳
                FasInspectionTask fasInspectionTask = new FasInspectionTask();
                fasInspectionTask.setDeptId(deptId);
                fasInspectionTask.setLineId(lineId);
                fasInspectionTask.setId(id);
                String inspectionDate = s+ ' ' + interval;
                String plannedCompletionTimeStr = s + ' ' + StrUtil.subAfter(interval, "-", false);
                Date plannedCompletionTime = DateUtils.parseDate(plannedCompletionTimeStr);
                fasInspectionTask.setInspectionDate(inspectionDate);
                fasInspectionTask.setPlannedCompletionTime(plannedCompletionTime);
                fasInspectionTask.setCreateTime(createTime);
                list.add(fasInspectionTask);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        return fasInspectionTaskMapper.batchInsertTask(list);
    }

    @Override
    public int deleteFasInspectionTask(Long[] lineIds) {
        return fasInspectionTaskMapper.deleteFasInspectionTask(lineIds);
    }

    @Override
    public List<FasInspectionTask> findAll(FasInspectionTask fasInspectionTask) {
        List<FasInspectionTask> list = fasInspectionTaskMapper.findAll(fasInspectionTask);
        for (FasInspectionTask inspectionTask : list) {// 查询合作单位名称 计算完成率
            inspectionTask.setCompanyName(FasCooperationCompanyServiceImpl.COMPANY_NAMES.get(inspectionTask.getCooperationCompanyId()));
            Integer taskNum = inspectionTask.getTaskNum();
            Integer lineCompleteNum = inspectionTask.getLineCompleteNum();
            double completeRate = getRate(inspectionTask.getTaskNum(),inspectionTask.getLineCompleteNum());
            int unCheckNum = taskNum - lineCompleteNum;
            int unCheckNum_ = Math.max(unCheckNum, 0);
            inspectionTask.setCompletionRate(completeRate);
            inspectionTask.setUnCheckNum(unCheckNum_);
        }
        return list;
    }

    private double getRate(Integer taskNum,Integer lineCompleteNum) {
        if (taskNum == null || taskNum == 0) return 0;
        return Arith.div(lineCompleteNum.doubleValue(),taskNum.doubleValue(),2);
    }

    @Override
    public int minappLineStart(Long id) {
        return fasInspectionTaskMapper.minappLineStart(id);
    }

    /**
     *  获取两个日期之间的所有日期 (年月日)
     *
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return 时间字符串数组
     */
    public List<String> getBetweenDate(Date startDate,Date endDate){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 声明保存日期集合
        List<String> list = new ArrayList<>();
        try {
            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime()<=endDate.getTime()){
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                // 设置日期
                calendar.setTime(startDate);
                //把日期增加一天
                calendar.add(Calendar.DATE, 1);
                // 获取增加后的日期
                startDate=calendar.getTime();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;

    }

}
